<?php

/**
 * @author      :   QuangLT
 * @name        :   NewsModel
 * @copyright   :   Fpt Online
 * @todo        :   News model
 */
class Statictis {

    protected static $_instance = null;

    /**
     * Define key cache for news detail
     * list_statistic:{categoryid}:{article_type}:{view|comment}
     */
    const LIST_STATISTIC = 'list_statistic';
    const SHARDING_ALL = 1;
    /**
     * Constructor of class
     * we don't permit an explicit call of the constructor! (like $v = new Singleton())
     */
    protected function __construct() {
        
    }

    /**
     * Get singletom instance
     * @return <object>
     */
    public final static function getInstance() {
        //Check instance
        if (is_null(self::$_instance)) {
            self::$_instance = new self();
        }

        //Return instance
        return self::$_instance;
    }

    /**
     * Get top view article
     * @param type $intSiteID
     * @param type $intArticleType
     * @param type $intTotal
     * @return array List ID of article
     */
    public function getTopViewArticle($intArticleType = null, $intCateID = SITE_ID, $limit = 5) 
    {
        try
        {
            //default response
            $arrReturn = false;
            $intCateID = intval($intCateID);
            $intArticleType = intval($intArticleType);
            $arrExcluse = Fpt_Global::getExcluses();
            if($arrExcluse['total'] > 0)
            {
                $originalLimit = $limit;
                //edit offset limit
                $limit+= intval($arrExcluse['total']);
            }
            
            //get memcache instance
            $memcacheInstance = Fpt_Global::getMemcacheInstance(self::SHARDING_ALL);
            //list_statistic:{categoryid}:{article_type}:{view|comment}
            $arrParam = array($intCateID, $intArticleType, 'view');
            $keyCache = Fpt_Global::makeCacheKey(self::LIST_STATISTIC, $arrParam);
            $arrReturn = $memcacheInstance->read($keyCache);
            
            if($arrReturn == false || empty($arrReturn) || count($arrReturn) < $limit )
            {
                // Get application config
                $config = Fpt_Global::getApplicationIni();
                //init mysql instance
                $statictisMysql = Fpt_Business_Statictis::factory($config['database']['default']);
                $intArticleType = $intArticleType>0?$intArticleType:NULL;
                //get db list article by time
                $arrReturn = $statictisMysql->getListArticleByView($intArticleType, NULL, $limit);
                if(!empty($arrReturn))
                {
                    Fpt_Global::writeMemcache(self::SHARDING_ALL, $keyCache, $arrReturn);
                }
            }//end check get redis failed?   
            if(is_array($arrReturn))
            {
                $arrReturn = array_slice($arrReturn, 0, $limit);
            }
            //check if have to excluse 
            if($arrExcluse['total'] > 0 && count($arrReturn) > 0)
            {
                //get diff articleIDs
                $arrResult = array_diff($arrReturn, $arrExcluse['data']);
                //then slice to get right limit
                $arrResult = array_slice($arrResult, 0, $originalLimit);
                //assign return
                $arrReturn = $arrResult;
                unset($arrResult);
            } 
        } 
        catch (Zend_Exception $ex) 
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $arrReturn;
    }

    /**
     * Get top comment article
     * @param type $intSiteID
     * @param type $intTotal 
     * @return array List ID of article
     */
    public function getTopCommentArticle($intSiteID = SITE_ID, $limit = 5) 
    {

        try
        {
            //default response
            $arrReturn = false;

            $arrExcluse = Fpt_Global::getExcluses();
            if($arrExcluse['total'] > 0)
            {
                $originalLimit = $limit;
                //edit offset limit
                $limit+= intval($arrExcluse['total']);
            }
            //get memcache instance
            $memcacheInstance = Fpt_Global::getMemcacheInstance(self::SHARDING_ALL);
            //init param key cache
            $arrParam = array(0, 0, 'comment');
            //make memcache key
            $keyCache = Fpt_Global::makeCacheKey(self::LIST_STATISTIC, $arrParam);
            //read list
            $arrReturn = $memcacheInstance->read($keyCache);
            
            if($arrReturn == false || empty($arrReturn) || count($arrReturn) < $limit )
            {
                // Get application config
                $config = Fpt_Global::getApplicationIni();
                //init mysql instance
                $statictisMysql = Fpt_Business_Statictis::factory($config['database']['default']);

                //get db list article by time
                $arrReturn = $statictisMysql->getListArticleByComment($limit);
                if(!empty($arrReturn))
                {
                    Fpt_Global::writeMemcache(self::SHARDING_ALL, $keyCache);
                }
            }//end check get redis failed?
            //check if have to excluse 
            if($arrExcluse['total'] > 0 && count($arrReturn) > 0)
            {
                //get diff articleIDs
                $arrResult = array_diff($arrReturn, $arrExcluse['data']);
                //then slice to get right limit
                $arrResult = array_slice($arrResult, 0, $originalLimit);
                //assign return
                $arrReturn = $arrResult;
                unset($arrResult);
            }
        } 
        catch (Zend_Exception $ex) 
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $arrReturn;
    }
    
    public function syncStatistic() 
    {
        try
        {
            //default response
            $arrReturn = false;
            // Get application config
            $config = Fpt_Global::getApplicationIni();
            //init mysql instance
            $statictisMysql = Fpt_Business_Statictis::factory($config['database']['default']);
            
            //get list article by view video only
            $arrViewVideo = $statictisMysql->getListArticleByView(VIDEO, NULL, 100);
            if($arrViewVideo !== false)
            {
                //list_statistic:{categoryid}:{article_type}:{view|comment}
                $arrParam = array(CATE_ID_VIDEO, VIDEO, 'view');
                $keyCache = Fpt_Global::makeCacheKey(self::LIST_STATISTIC, $arrParam);
                $arrReturn[] = Fpt_Global::writeMemcache(self::SHARDING_ALL, $keyCache, $arrViewVideo);
            }
            
            //get db list article by view all
            $arrView = $statictisMysql->getListArticleByView(null, NULL, 100);
            if($arrView !== false)
            {
                $arrParam = array(SITE_ID, 0, 'view');
                $keyCache = Fpt_Global::makeCacheKey(self::LIST_STATISTIC, $arrParam);
                $arrReturn[] =  Fpt_Global::writeMemcache(self::SHARDING_ALL, $keyCache, $arrView);
            }
            
            //get db list article by comment
            $arrComment = $statictisMysql->getListArticleByComment(100);
            if($arrComment !== false)
            {
                $arrParam = array(SITE_ID, 0, 'comment');
                $keyCache = Fpt_Global::makeCacheKey(self::LIST_STATISTIC, $arrParam);
                $arrReturn[] =  Fpt_Global::writeMemcache(self::SHARDING_ALL, $keyCache, $arrComment);
            }               
        } 
        catch (Zend_Exception $ex) 
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $arrReturn;
    }

}